package com.maxd.service.helpTask;

import com.maxd.model.HelpClassify;
import com.maxd.model.HelpTaskPlatform;
import com.maxd.respository.helpTask.HelpTaskPlatformJpaRepository;
import com.maxd.respository.helpTask.HelpTaskPlatformRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class HelpTaskPlatformServiceImpl implements IHelpTaskPlatformService {

    private final HelpTaskPlatformJpaRepository helpTaskPlatformJpaRepository;
    private final HelpTaskPlatformRepository helpTaskPlatformRepository;
    private final IHelpClassifyService iHelpClassifyService;

    @Autowired
    public HelpTaskPlatformServiceImpl(HelpTaskPlatformJpaRepository helpTaskPlatformJpaRepository, HelpTaskPlatformRepository helpTaskPlatformRepository, IHelpClassifyService iHelpClassifyService) {
        this.helpTaskPlatformJpaRepository = helpTaskPlatformJpaRepository;
        this.helpTaskPlatformRepository = helpTaskPlatformRepository;
        this.iHelpClassifyService = iHelpClassifyService;
    }

    @Override
    public HelpTaskPlatform saveBody(HelpTaskPlatform helpTaskPlatform){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        helpTaskPlatform.setCreateTime(sdf.format(new Date()));
        helpTaskPlatform.setState(1);
        return helpTaskPlatformJpaRepository.save(helpTaskPlatform);
    }

    @Override
    public HelpTaskPlatform updateHelpTaskPlatform(HelpTaskPlatform helpTaskPlatform){
        return helpTaskPlatformJpaRepository.save(helpTaskPlatform);
    }

    @Override
    public void deleteHelpTaskPlatformById(Long id){
        helpTaskPlatformJpaRepository.deleteById(id);
    }

    @Override
    @Transactional
    public void deleteHelpTaskPlatformByIds(String ids){
        for(String id:ids.split(",")){
            helpTaskPlatformJpaRepository.deleteById(Long.parseLong(id));
        }
    }

    @Override
    public Page<HelpTaskPlatform> selectHelpTaskPlatformListByState(Pageable pageable, Integer state){
        if(state==null||state==-1){
            Page<HelpTaskPlatform> helpTaskPlatforms = helpTaskPlatformRepository.selectHelpTaskPlatformListByState(pageable);
            List<HelpTaskPlatform> content = helpTaskPlatforms.getContent();
            List<HelpClassify> helpClassifies = iHelpClassifyService.selectList(1);
            for(HelpTaskPlatform helpTaskPlatform:content){
                for(HelpClassify classify:helpClassifies){
                    if(helpTaskPlatform.getClassifyId().equals(classify.getId())){
                        helpTaskPlatform.setClassifyName(classify.getClassifyName());
                        break;
                    }
                }
            }
            return helpTaskPlatforms;
        }
        return helpTaskPlatformRepository.selectHelpTaskPlatformListByState(pageable,state);
    }

    @Override
    public Page<HelpTaskPlatform> selectHelpTaskPlatformList(Pageable pageable,Long classifyId){
        Page<HelpTaskPlatform> helpTaskPlatforms = helpTaskPlatformRepository.selectHelpTaskPlatformList(pageable, classifyId);
        List<HelpTaskPlatform> content = helpTaskPlatforms.getContent();
        HelpClassify helpClassify = iHelpClassifyService.selectById(classifyId);
        for(HelpTaskPlatform helpTaskPlatform:content){
            helpTaskPlatform.setClassifyName(helpClassify.getClassifyName());
        }
        return helpTaskPlatforms;
    }

    @Override
    public HelpTaskPlatform selectById(Long id){
        return helpTaskPlatformJpaRepository.getOne(id);
    }

    @Override
    public int updateHelpTaskPlatformStateById(Long id,Integer state){
        return helpTaskPlatformRepository.updateHelpTaskPlatformStateById(id,state);
    }




}
